package com.example.administrator.wmsdemo;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class WMSService extends Service implements View.OnClickListener {

    private WindowManager wm;
    private WindowManager.LayoutParams wParams;
    private JohnLinearLayout view;
    private Button btn;
    private LayoutInflater inflater;

    public static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    public static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    public WMSService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        inflater = LayoutInflater.from(this);
        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        registerReceiver(receiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initWindow();
        wm.addView(view, wParams);//添加窗口
        return super.onStartCommand(intent, flags, startId);
    }


    /**
     * 设置窗口
     */
    private void initWindow() {
        view = (JohnLinearLayout) inflater.inflate(R.layout.sss, null);

        btn = (Button) view.findViewById(R.id.bt);


        wParams = new WindowManager.LayoutParams();




//        wParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        wParams.type = WindowManager.LayoutParams.TYPE_PHONE;

        wParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;//设置全屏

//        wParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;


        wParams.format = PixelFormat.TRANSLUCENT;//支持透明

//        wParams.alpha = 0.5f;

        btn.setOnClickListener(this);

        view.setmDispatchKeyEventListener(dispatchKeyEventListener);
    }


    JohnLinearLayout.DispatchKeyEventListener dispatchKeyEventListener = new JohnLinearLayout.DispatchKeyEventListener() {
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                if (view.getParent() != null) {
                    wm.removeView(view);
                }
                return true;
            }

            return false;
        }
    };


    @Override
    public void onDestroy() {

        unregisterReceiver(receiver);

        super.onDestroy();
        if (view.getParent() != null) {
            wm.removeView(view);
        }
        Log.e("JohnLog", "---onDestroy----");
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.bt:
                if (view.getParent() != null) {
                    wm.removeView(view);
                    Log.e("JohnLog", "---removeView----");
                }
                stopSelf();
                break;
        }
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                    Toast.makeText(context, "Home按键", Toast.LENGTH_SHORT).show();
                    if (view.getParent() != null) {
                        wm.removeView(view);
                    }
                }
            }
        }
    };
}
